home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-04
/
ndx300.zip
/
NDX.H
< prev
next >
Wrap
C/C++ Source or Header
|
1992-10-29
|
4KB
|
101 lines
/* NDX.H . . . Header file for index package */
#ifndef NDX_H
#define NDX_H
#include <string.h>
#include <stdio.h>
#include <dir.h>
typedef unsigned WORD;
typedef unsigned long DWORD;
typedef unsigned char BYTE;
const unsigned stackdepth = 10; // Maximum tree height
const unsigned nodesize = 1024; // Bytes in each node
const unsigned maxkey = 128; // Maximum key length
const unsigned maxendkeys = nodesize - sizeof(WORD) - sizeof(DWORD);
// Maximum key data length
enum types {NODUPS, FIFO, LIFO}; // File access modes
typedef int (*CFN)(char *, char *, ...); // Comparison function types
class Key { // Key structure
#ifdef TEST
public:
#endif
friend Node;
friend Index;
DWORD lson; // Pointer to left son (seek address in file)
DWORD offset; // Record seek address or number
char data[maxkey]; // ASCIZ key
DWORD insert(DWORD root);
Key(){}
Key(char *, DWORD);
void push(Node *);
Node * pop(Key *&);
Key *prev(Node *); // Returns previous Key in the Node (not the Index)
Key *next(){return (Key *)((char *)this + length());} // The next one
BYTE length() {return 2*sizeof(DWORD) + strlen(data) + 1;}
// Length of the Key
Key *keycpy(Key *s){return (Key *)(memcpy(this, s, s->length()));}
};
class Node {
friend Key;
friend Node;
friend Index;
DWORD offset; // Node offset or free node chain
unsigned endkeys; // First unused data byte in keys
char keys[maxendkeys]; // The Key structures
DWORD *rson(){return (DWORD *)(keys + endkeys);} // Pointer to right son
split(); // Split node in two
void shiftup(Key *, unsigned); // Make room for key
void shiftdn(Key *, unsigned, unsigned); // Delete key
};
class Frame { // State stack frame
friend Key;
friend Node;
DWORD node; // Offset of node in the index file
WORD offset; // Offset of the Key in that node
};
class Index { // THE index structure
friend Key;
friend Node;
DWORD root; // File offset of the root node
DWORD eof; // File offset of the end of file
DWORD freelist; // File offset of free node list
BYTE minor, // Version number
major,
mode, // File access mode
stacktop; // Current stack top index
Key ckey; // Current key structure
Frame stack[stackdepth]; // Current state
CFN cmpfn; // find() comparison function
char filename[MAXPATH]; // Index filename
FILE *handle; // Index file handle
initcache();
Node *newnode();
Node *getnode(DWORD);
DWORD _find(char *, DWORD);
void read(DWORD, void *, unsigned);
void write(DWORD, void *, unsigned);
public:
static unsigned hdrsize;
void print(char *);
Index(char *, int, CFN = (CFN)strcmp); // Constructor to create index
Index(char *, CFN = (CFN)strcmp); // Constructor to open old one
~Index(); // Destructor, closes index
DWORD insert(char *, DWORD); // Insert new key
int remove(char *, DWORD); // Remove key
DWORD find(char *); // Find key
DWORD findkeyrec(char *, DWORD); // Find key, specific record
DWORD first(unsigned = 0); // First key
DWORD next(); // Next key
DWORD prev(); // Previous key
};
#endif NDX_H